//
//  Team9ExpertDiscussTableViewCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertDiscussTableViewCell.h"
#import "Team9ExpertDiscussCell.h"
#import <Masonry.h>

@interface Team9ExpertDiscussTableViewCell ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@end
@implementation Team9ExpertDiscussTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self addSubview:_tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
//    self.tableView.allowsSelection = NO;
    self.tableView.scrollEnabled = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 150 * WIDTH/ 375;
    self.tableView.tableFooterView = [UIView new];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = 5;
    [button setTitle:@"更多讨论" forState:UIControlStateNormal];
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor grayColor];
        self.tableView.backgroundColor = [UIColor blackColor];
    } else {
        color = [UIColor orangeColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    [button setTitleColor:color forState:UIControlStateNormal];
    button.layer.borderColor = color.CGColor;
    button.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    button.layer.borderWidth = 1;
    [self.tableView addSubview:button];
    [button addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-10);
        make.height.equalTo(@(WIDTH / 375 * 30));
        make.width.equalTo(button.mas_height).multipliedBy(3);
    }];

    
}
- (void)more{
    Team9ExpertMoreTopicVC *moereVC = [Team9ExpertMoreTopicVC new];
    moereVC.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_topic_list?user_id=%@&page=1", _Id];
    self.vc.hidesBottomBarWhenPushed = YES;
    [self.vc.navigationController pushViewController:moereVC animated:YES];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.tableView.frame = self.contentView.frame;
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//#warning test!
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"discussReuse";
    Team9ExpertDiscussCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[Team9ExpertDiscussCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.model = self.array[indexPath.row];
    return cell;
}
- (void)setArray:(NSArray *)array
{
    if (_array != array) {
        _array = array;
        [self.tableView reloadData];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"test2");
    if (_block) {
        self.block(indexPath.row);
    }
}
@end
